﻿	
	window.onresize=function(){
		var entryDiv = document.getElementById('entryDiv');
		var win_Width = document.body.clientWidth;
		entryDiv.style.width =win_Width-5;
	}	
	
	Ext.onReady(function() {	
		var entryDiv = document.getElementById('entryDiv');
		var win_Width = document.body.clientWidth;
		entryDiv.style.width =win_Width-5;
		
		var allTotal=0;
		document.getElementById('spAllOTsum').innerText=allTotal;
		
		editGrid.on("beforeedit", function(e) {
			var field = e.field; // current field's id
			var rowIndex = e.row; // current row number
			var record = e.record; // current record data
			var colIndex = e.column; // current column number		
			var grid = e.grid; // current grid data	
			var cm = grid.getColumnModel(); // grid column model
			var editor = cm.getCellEditor(colIndex, rowIndex);
			var holidayName=record.get('holidayTypeName');
			if(field == 'startTime')
			{
				if(holidayName==null||holidayName=='')
				{
				   alert('请选择假期类型!');
				   return false;
				}else{
				   if(holidayName=='忘打卡')
				   {
					   alert('请填写忘打卡时间!');
					   editGrid.stopEditing();
					   return false;				       
				   }
				}
			}
			if(field == 'endTime')
			{
				if(holidayName==null||holidayName=='')
				{
				   alert('请选择假期类型!');
				   return false;
				}else{
				   if(holidayName=='忘打卡')
				   {
					   alert('请填写忘打卡时间!');
					   editGrid.stopEditing();
					   return false;				       
				   }
				}
			}
			
			if(field == 'signedCard')
			{
		
			   if(holidayName!='忘打卡')
			   {
				   editGrid.stopEditing();
				   return false;				       
			   }
				
			}
			
		});
		
		editGrid.on("afteredit", function(e) {
			var field = e.field; // current field's id
			var record = e.record; // current record data
			var rowIndex = e.row; // current row number
			var colIndex = e.column; // current column number		
			var grid = e.grid; // current grid data	
			var cm = grid.getColumnModel(); // grid column model
			var editor = cm.getCellEditor(colIndex, rowIndex);
			if (field == 'bizDate') { 
				var holiday=record.get("bizDate");			
				getHolidayHouts(holiday,record);
			}
			if (field == 'holidayType') { 
				var fieldOV = editor.field.objectValues;
				var holidayType='';
				var holidayTypeStr='';
				if(fieldOV=='')
				{
				  return;
				}
				holidayTypeStr=fieldOV[0].data.text["name"];
				record.set("holidayTypeStr", holidayTypeStr);
				holidayType=fieldOV[0].data.text["htype"];
				record.set("holidayTypeName", holidayType);
				if(holidayType=='忘打卡')
				{
					record.set("startTime", '');	
					record.set("endTime", '');	
					record.set("totalhour", 0);				
				}
			}
			var restStart=Number(record.get("restStart"));
			var restEnd=Number(record.get("restEnd"));
			var workHours=Number(record.get("workHours"));
			var restTime=(restEnd-restStart)/60;
			if (field == 'startTime') { 
				var startTime=record.get("startTime"); 
				if(startTime!='')
				{
					var startArray=startTime.split(":");
					if(startArray[1]*60!=0&&startArray[1]*60!=1800)
					{
						record.set("totalHour", 0);
						record.set("startTime", '');	
						return false;
					}				
				}else
				{
					record.set("totalHour", 0);
				}	
				var endTime=record.get("endTime"); 	
				if(endTime==null)
                {
				   endTime=''
				}				
				if(startTime!=''&&endTime!='')
				{
					var startArray=startTime.split(":");
					var endArray=endTime.split(":");
					var startSS=Number(startArray[0]*60)+Number(startArray[1]);
					var endSS=Number(endArray[0]*60)+Number(endArray[1]);
					if(startSS>endSS)
					{
					  alert('开始时间不能大于结束时间');
					  return false;
					}
					var fristTime=0;
					var secondTime=0;
					if(startSS<=restStart)
					{
					    if(endSS<restStart)
						{
							fristTime=Number(endSS-startSS)/60;
						}else
						{
							fristTime=Number(restStart-startSS)/60;
						}
					}
					if(restEnd<=endSS)
					{
						if(restEnd<startSS)
						{
							secondTime=Number(endSS-startSS)/60;
						}else
						{
							secondTime=Number(endSS-restEnd)/60;	
						}				
					}
					var hours=fristTime+secondTime;
					if(hours>workHours)
					{
					   alert('总时间超过'+workHours+'小时，请确认！');
					}
					record.set("totalhour", hours);
				}			
			}
			if (field == 'endTime') { 
				var startTime=record.get("startTime"); 	
				var endTime=record.get("endTime"); 		
				if(endTime!='')
				{
					var endArray=endTime.split(":");
					if(endArray[1]*60!=0&&endArray[1]*60!=1800)
					{
						record.set("totalHour", 0);
						record.set("endTime", '');	
						return false;
					}				
				}else
				{
					record.set("totalHour", 0);
				}					
				if(startTime!=''&&endTime!='')
				{
					var startArray=startTime.split(":");
					var endArray=endTime.split(":");
					var startSS=Number(startArray[0]*60)+Number(startArray[1]);
					var endSS=Number(endArray[0]*60)+Number(endArray[1]);
					if(startSS>endSS)
					{
					  alert('开始时间不能大于结束时间');
					  return false;
					}
					var fristTime=0;
					var secondTime=0;
					if(startSS<=restStart)
					{
					    if(endSS<restStart)
						{
							fristTime=Number(endSS-startSS)/60;
						}else
						{
							fristTime=Number(restStart-startSS)/60;
						}
					}
					if(restEnd<=endSS)
					{
						if(restEnd<startSS)
						{
							secondTime=Number(endSS-startSS)/60;
						}else
						{
							secondTime=Number(endSS-restEnd)/60;	
						}				
					}
					var hours=fristTime+secondTime;
					if(hours>workHours)
					{
					   alert('总时间超过'+workHours+'小时，请确认！');
					}
					record.set("totalhour", hours);
				}else 
				{
				   if(startTime=='')
				   {
				      alert('请输入开始时间!');
					  return false;
				   }
				   if(endTime=='')
				   {
				      alert('请输入结束时间!');
					  return false;
				   }
				}				
			}
			sumHour();
		});		
	});
	
	
	function sumHourRenderer(value, metadata, record,rowIndex,colIndex) {
		var totalhour=record.get("totalhour");
		var allTotal=parseFloat(document.getElementById('spAllOTsum').innerText);	
		if(totalhour!=null)
		{
		   if(isNaN(totalhour))
		   {
		      totalhour=0;
		   }
		   if(isNaN(allTotal))
		   {
		      allTotal=0;
		   }
		   allTotal=Number(allTotal+totalhour);
		   document.getElementById('spAllOTsum').innerText=allTotal;	
		}
	   return value;
	}	
	
	function sumOT()
	{
		
		var normalOT=document.getElementById('spNormalOT');	
		var weekenOT=document.getElementById('spWeekenOT');	
		var holidayOT=document.getElementById('spHolidayOT');	
		var normalOTInt=parseFloat(normalOT.innerText);
		var weekenOTInt=parseFloat(weekenOT.innerText);
		var holidayOTInt=parseFloat(holidayOT.innerText);
		return normalOTInt+weekenOTInt+holidayOTInt;
	}
	
	function sumHour()
	{
		var store=editGrid.getStore();
	    var totalday=0;
		var totalweek=0;
		var totalholiday=0;
		var allTotal=0;
		for(var i=0;i<store.data.length;i++){
			var totalhour=store.getAt(i).data['totalhour']; 
			if(totalhour==null||totalhour=="")
			{
				totalhour=0;
			}
			allTotal=Number(allTotal+totalhour);
		}
		document.getElementById('spAllOTsum').innerText=allTotal;	
		//document.getElementById('spAllOTsum').innerText=allTotal;	
		//document.getElementById('spNormalOTsum').innerText=totalday;	
		//document.getElementById('spWeekenOTsum').innerText=totalweek;	
		//document.getElementById('spHolidayOTsum').innerText=totalholiday;
	}
	
	function sumHour1()
	{
		var store=editGrid.getStore();
	    var totalday=0;
		var totalweek=0;
		var totalholiday=0;
		var allTotal=0;
		for(var i=0;i<store.data.length;i++){
			var holidayTypeStr = store.getAt(i).data['holidayTypeStr'];
			if(isNotEmpty(holidayTypeStr))
			{
			   if(holidayTypeStr=='工作日加班调休')
			   {
					var totalhour=store.getAt(i).data['totalhour']; 
					if(totalhour==null||totalhour=="")
					{
						totalhour=0;
					}
					totalday=Number(totalhour+totalday);
			   }else 
			   if(holidayTypeStr=='周末加班调休')
			   {
					var totalhour=store.getAt(i).data['totalhour']; 
					if(totalhour==null||totalhour=="")
					{
						totalhour=0;
					}
					totalweek=Number(totalhour+totalweek);
			   }else
			   if(holidayTypeStr=='节假日加班调休')
			   {
					var totalhour=store.getAt(i).data['totalhour']; 
					if(totalhour==null||totalhour=="")
					{
						totalhour=0;
					}
					totalholiday=Number(totalhour+totalholiday);
			   }else
			   {
					var totalhour=store.getAt(i).data['totalhour']; 
					if(totalhour==null||totalhour=="")
					{
						totalhour=0;
					}
					allTotal=Number(totalhour+allTotal);
			   }
			}
		}
		document.getElementById('spAllOTsum').innerText=allTotal+totalholiday+totalweek+totalday;	
		//document.getElementById('spAllOTsum').innerText=allTotal;	
		//document.getElementById('spNormalOTsum').innerText=totalday;	
		//document.getElementById('spWeekenOTsum').innerText=totalweek;	
		//document.getElementById('spHolidayOTsum').innerText=totalholiday;
	}	
	
	function showProcessDiagramWin()
	{	
		var bosID = encodeId(txtfid.value);
		KDWin.open('../../../../workflow/viewProcessDiagramWl.jsf?billId=' + bosID,{name:'',title:'',X:960,Y:720,scroll:'yes',resizable:'yes',Maximize:'no',Minimize:'no'});
	}	
	
	function forwardLongHoliday() {
		KDWin.openDialog('../bizbill/longHolidayEditui.jsf?operateState=ADDNEW', {name:'editBill_LongHolidayEdituiBean',title:'',X:600,Y:280,scroll:'off',resizable:'yes',Maximize:'no',Minimize:'no'});
	}
	
	function checkEntry(){
		var store=editGrid.getStore();
		if(store.data.length<1){
		  alert("保存失败：分录不能为空！");
		  return false;
		}
		for(var i=0;i<store.data.length;i++){
			var bizDate = store.getAt(i).data['bizDate'];
			var holidayType = store.getAt(i).data['holidayType'];
			var holidayTypeName = store.getAt(i).data['holidayTypeName'];
		    var row = i+1;
			if(bizDate==null||bizDate==""||bizDate==undefined)
			{
			 alert("保存失败！\n分录第【"+row+"】行请假日期为空！");
			 return false;
		   }else if(holidayType==null||holidayType==""||holidayType==undefined){
		     alert("保存失败！\n分录第【"+row+"】行假期类型为空！");
			 return false;
		   }else if(holidayTypeName=='忘打卡')
		   {
		      //在隐藏列
			  var signedCard = store.getAt(i).data['signedCard'];
			  if(!isNotEmpty(signedCard))
			  {
				 alert("保存失败！\n分录第【"+row+"】行忘打卡时间为空！");
			     return false;
			  }
		   }else if(holidayTypeName=='工出')
		   {
			  var address = store.getAt(i).data['address'];
			  var workDetail = store.getAt(i).data['workDetail'];
			  if(!isNotEmpty(address))
			  {
				 alert("保存失败！\n分录第【"+row+"】行公出地点为空！");
			     return false;
			  }
			  if(!isNotEmpty(workDetail))
			  {
				 alert("保存失败！\n分录第【"+row+"】行工作事项为空！");
			     return false;
			  }
		   }else if(holidayTypeName=='调休假')
		   {
				var balanceOT=sumOT();
				var otNegative =document.getElementById("editForm:txtOtNegative").value;
				var totalhour=store.getAt(i).data['totalhour']; 		
				if(otNegative==0)
				{
				  //不可透支
				  if(balanceOT<totalhour)
				  {
					 alert("保存失败！\n分录第【"+row+"】行调休小时数超过剩余加班数！");
					 return false;				  
				  }
				}				
		   }else{
				var startTime = store.getAt(i).data['startTime'];
				var endTime = store.getAt(i).data['endTime'];
			    if(!isNotEmpty(startTime))
			   {
				 alert("保存失败！\n分录第【"+row+"】行开始时间为空！");
			     return false;
			   }
				if(!isNotEmpty(endTime))
			   {
				 alert("保存失败！\n分录第【"+row+"】行结束时间为空！");
			     return false;
			   }
		   }
		}
	    return true;
	}
	
	function addEntry(isDisabled)
	{
		if(isDisabled=='true')
		{
		  return false;
		}
		/*var pkstartDate =document.getElementById('editForm:txtStartdate').value;
		var pkendDate =document.getElementById('editForm:txtendDate').value;
		if(!isNotEmpty(pkstartDate))
		{
		  alert('开始时间不能为空!');
		  return false;
		}
		if(!isNotEmpty(pkendDate))
		{
		  alert('结束时间不能为空!');
		  return false;
		}*/
		document.getElementById("editForm:appendRow").onclick(); 	
	}
	
	function autoHideMsgArea(){
	    var i =0;
		i++;
	}
	
	function btnMySave(){
		/*var pkstartDate =document.getElementById('editForm:txtStartdate').value;
		var pkendDate =document.getElementById('editForm:txtendDate').value;
		if(!isNotEmpty(pkstartDate))
		{
		  alert('开始时间不能为空!');
		  return false;
		}
		if(!isNotEmpty(pkendDate))
		{
		  alert('结束时间不能为空!');
		  return false;
		}*/
		if(checkEntry())
		{
			btnSave.fireEvent("click");
		}
	 }
	 
	function btnMySumit(){
		/*var pkstartDate =document.getElementById('editForm:txtStartdate').value;
		var pkendDate =document.getElementById('editForm:txtendDate').value;
		if(!isNotEmpty(pkstartDate))
		{
		  alert('开始时间不能为空!');
		  return false;
		}
		if(!isNotEmpty(pkendDate))
		{
		  alert('结束时间不能为空!');
		  return false;
		}*/
		if(checkEntry())
		{
			btnSubmit.fireEvent("click");
		}
	 }
	 
	function isNotEmpty(valueObj)
	{
		if(valueObj==null||valueObj==""||valueObj==undefined){
		   return false;
		}else
		{
		   return true;
		}
	}
	
	
	function addLongHolidayRowCall() {
		document.getElementById("editForm:roadListModel").onclick(); 
	}	

	
	/**
	*用于转化ID 含有加好
	*/
	function encodeId(objId)
	{
		if(objId==null||objId==""){
			return objId;
		}else{
		 return objId.replace(/\+/g,'%2B');
		}
	}
	
//	----------------------------------------AJAX  start---------------------------------------------

function getHolidayHouts(holiday,record)
{
	var params = {
		getDataMethod : 'getScheduleInfo',
		holiday:holiday
	};
	var newJsonObject="";
	
	Ext.Ajax.request({
		url:'../bizbill/ajaxAction.jsf',
		method:'post',
		success : 
			function(response, request){
				var jsonRaw = parseAjaxSubmitReturnValue(response.responseText);
				if(jsonRaw=='')
				{
				  //默认集团班表
					record.set("restStart",720);
					record.set("restEnd", 810);	
					record.set("workHours", 7.5);	
					editGrid.view.onUpdate(editGrid.getStore(), record);		
                    alert('今天没有配置班表,请联系考勤管理员');		
                    return false;					
				}
				newJsonObject= Ext.util.JSON.decode(jsonRaw);
				record.set("restStart",Number(newJsonObject.restStart));
				record.set("restEnd", Number(newJsonObject.restEnd));	
				record.set("workHours", Number(newJsonObject.workHours));	
				editGrid.view.onUpdate(editGrid.getStore(), record);					
			},
		failure : error,
		params : params,
		scope : this,
		timeout : 30000
	});
	return newJsonObject;
}

function parseAjaxSubmitReturnValue(responseText) {
	var returnString = "";
	if (responseText) {
		// 去除html标记
		// var re = /<(.*)>.*<\/\1>/gi;
		var AJAXSUBMIT_PREFIX="ajaxsubmitReturnValue=";
		var index = responseText
				.lastIndexOf(AJAXSUBMIT_PREFIX); // length:22
		if (index > -1) {
			// 跳过返回值的前缀
			returnString = responseText.substring(index
					+ AJAXSUBMIT_PREFIX.length);
			var re = /\s*<\s*(script)(.|\n)*\/\1\s*>s*/gi;
			returnString = returnString.replace(re, "");
		}
	}
	return returnString;
}

/**
 * 执行Ajax失败函数
 */
function error(response, request) {
	alert("操作超时!");
}	

//--------------------------------------------AJAX end ---------------------------------------------